<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

defined('ANS') or die();
?>

<script type="text/javascript">

var languages = new Array;

<?php
foreach ($download_languages as $language => $files) {
	echo "\n".'languages["'.$language.'"] = new Array;';
	echo "\n".'languages["'.$language.'"][0] = "";';

	foreach ($files as $index => $each) {
		echo "\n".'languages["'.$language.'"]['.$index.' + 1] = "'.$each.'";';
	}
}
?>

$(document).ready(function () {
	$('select[name$=_language]').change(function () {
		var name = $(this).attr('name').replace(/_.*$/, '');
		var val = $(this).val();

		$('#'+name+'_file option').remove();

		if (val == '') {
			$('#'+name+'_submit').attr('disabled', 'disabled');

			$('#'+name+'_file').append('<option value=""><?php __e('Select language first'); ?></option>');

			return false;
		}

		var cnt = languages[val].length;

		if (cnt > 1) {
			var options = "";

			for (var i = 1; i < cnt; i++) {
				options += '<option value="'+languages[val][i]+'">'+languages[val][i]+'</option>';
			}

			$('#'+name+'_file').append(options);

			$('#'+name+'_submit').attr('disabled', '');
		} else {
			$('#'+name+'_submit').attr('disabled', 'disabled');

			$('#'+name+'_file').append('<option value=""><?php __e('No valid files exist for this language'); ?></option>');
		}
	});
});

</script>

<div class="title">
	<h2><?php __e('Manage GetText files'); ?></h2>
	<p><?php __e('Download or upload the available language from gettext files'); ?></p>
</div>

<div>
	&nbsp;
</div>

<div>
	<form action="<?php echo path(); ?>" method="post" enctype="multipart/form-data">
		<div>
			<h3><?php __e('Download .po file'); ?></h3>

			<fieldset class="inline">
				<label for="download_language"><?php __e('Choose the language'); ?></label>

				<select name="download_language" id="download_language">
					<option value=""><?php __e('Select language'); ?></option>

					<?php foreach (array_keys($download_languages) as $language) { ?>
					<option value="<?php echo $language; ?>"><?php echo $language; ?></option>
					<?php } ?>
				</select>
			</fieldset>

			<fieldset class="inline">
				<label for="download_file"><?php __e('Choose the file'); ?></label>

				<select name="download_file" id="download_file">
					<option value=""><?php __e('Select language first'); ?></option>
				</select>
			</fieldset>

			<fieldset class="inline">
				<?php echo $Form->submit(array('value' => __('Download'), 'id' => 'download_submit', 'name' => 'phpcan_action[gettext-download]', 'disabled' => 'disabled')); ?>
			</fieldset>
		</div>

		<div>
			<h3><?php __e('Upload .po file'); ?></h3>

			<fieldset class="inline">
				<label for="upload_language"><?php __e('Choose the language'); ?></label>

				<select name="upload_language" id="upload_language">
					<option value=""><?php __e('Select language'); ?></option>

					<?php foreach (array_keys($download_languages) as $language) { ?>
					<option value="<?php echo $language; ?>"><?php echo $language; ?></option>
					<?php } ?>
				</select>
			</fieldset>

			<fieldset class="inline">
				<label for="upload_file"><?php __e('Choose the file'); ?></label>

				<select name="upload_file" id="upload_file">
					<option value=""><?php __e('Select language first'); ?></option>
				</select>
			</fieldset>

			<fieldset class="inline">
				<?php echo $Form->file('file'); ?> 
				<?php echo $Form->submit(array('value' => __('Upload'), 'id' => 'upload_submit', 'name' => 'phpcan_action[gettext-upload]', 'disabled' => 'disabled')); ?>
			</fieldset>
		</div>
	</form>
</div>
